/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import edu.albany.csi518.sick.DiseaseSymptom;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author anthony
 */
public class search extends HttpServlet {
//    DiseaseSymptom disease_symptom = null;
//    
//    /**
//     *
//     * @throws ServletException
//     */
//    @Override
//    public void init() throws ServletException {
//        disease_symptom = new DiseaseSymptom();
//        if (disease_symptom == null){
//            throw new ServletException();
//        }
//    }
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        DiseaseSymptom disease_symptom = new DiseaseSymptom();
        HttpSession session = request.getSession();
        
        //Query query = new Query();
        
        boolean debugger = false;
        
        String search_val = (String) request.getParameter("search_val");
        String action = (String) request.getParameter("action");
                 
        
        if (search_val == null)
            search_val = "";
        
        if (action == null)
            action = "";
        
        session.setAttribute("dis_symp_obj", disease_symptom);
        
        switch (action) {
            case "Search Diseases":
                disease_symptom.analyzeInput(search_val, debugger, true);
                break;
            case "Search Symptoms":
                disease_symptom.analyzeInput(search_val, debugger, false);
                break;
        }
        
        try {
                        
            Iterator it = disease_symptom.getIt();
            out.print("<h2>Result</h2>");
            if(it.hasNext()) {
                
                /* search symptoms */
                if (action.compareTo("Search Symptoms") == 0){
                    
                    Iterator<Map.Entry<String, Integer>> symIt = disease_symptom.getRankedSymtomsIt();
                    Map.Entry<String, Integer> next;
                    
                    out.println("<form action=\"RefineSymptoms\" method=\"POST\" >");
                    for (int i = 1; symIt.hasNext(); i++) {
                        next = (Map.Entry<String, Integer>) symIt.next();
                        
                        
                        
                        out.println("<input class=\"check\" type=\"checkbox\" name=\"" + next.getKey() + "\" />" + next.getKey() +"</br>");
                    }
                    
                    out.println("<input id=\"selectSymptom\" type=\"submit\" value=\"Select Symptoms\" onclick=\"refineSymptoms()\"/>");
                    /*while (it.hasNext()) {
                        String key = (String) it.next();
                        ArrayList<String> syms = disease_symptom.getVal(key);
                        out.println("<h3>" + key + "</h3>");
                        String sym = "<p>";
                        for (String s : syms) {
                            sym += "<a href=\"\">" + s + "</a>, ";
                            //out.println("<p>" + s + "</p>");
                        }
                        sym += "</p>";
                        out.print(sym);
                    }*/
                }
                
                /* search diseases */
                else{
                     /* checkbox form header */
                    out.println("<form action=\"RefineSymptoms\" method=\"POST\" >");
                    while (it.hasNext()) {
                        String key = (String) it.next();    //disease from hashmap key
                        
                        out.println("<input class=\"check\" type=\"checkbox\" name=\"" + key + "\" />" + key +"</br>");
                        
                        /* don't need the symptoms? */
                        //ArrayList<String> syms = disease_symptom.getVal(key);
                                                
                        /* print symptoms for disease */
                        /*String sym = "<p>";
                        for (String s : syms) {
                            sym += "<a href=\"\">" + s + "</a>, ";
                            //out.println("<p>" + s + "</p>");
                        }
                        sym += "</p>";
                        out.print(sym);*/

                    }
                    
                    out.println("<input id=\"selectDisease\" type=\"submit\" value=\"Select Diseases\" onclick=\"refine()\"/>");
                }
                //out.println("</body>");
                //out.println("</html>");
            }
            else {
                out.print("<p>No result was found for " + search_val);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
//        String text = "Hello world";
//
//        response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
//        response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
//        response.getWriter().write(text);       // Write response body.
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        /**
        String t = request.getParameter("test").toString();
        PrintWriter p = response.getWriter();
        System.out.println("Does this work?! " + t);
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormat.format(date));
        p.write(t + dateFormat.format(date));
        * */
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
